package com.java.controller;

import com.java.dao.ApplyMapper;
import com.java.model.*;
import com.java.service.AdvertisersAccountService;
import com.java.service.BookingService;
import com.java.service.AccountWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

/**
 * 广告主的订单投放
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private BookingService bookingService;

    @Autowired
    private AdvertisersAccountService advertisersAccountService;

    @Autowired
    private AccountWalletService accountWalletService;

    /**
     * 待投放
     * @return
     */
    @PostMapping("/orderList")
    @ResponseBody
    public Result orderList(Booking booking) {
        return bookingService.orderList(booking);
    }
    /**
     * 已投放
     * @return
     */
    @PostMapping("/orderList2")
    @ResponseBody
    public Result orderList2(Booking booking) {
        return bookingService.orderList2(booking);
    }
    /**
     * 已完成
     * @return
     */
    @PostMapping("/orderList3")
    @ResponseBody
    public Result orderList3(Booking booking) {
        return bookingService.orderList3(booking);
    }

    /**
     * 同意
     * @return
     */
    @PostMapping("/yes")
    @ResponseBody
    public Result yes(int id, int accountId, int ids, BigDecimal price) {
        Booking booking = new Booking();
        booking.setStatus(3);
        booking.setId(id);
        bookingService.yes(booking);

        //查出自己的冻结余额是多少
        AdvertisersAccount recharge = advertisersAccountService.recharge(accountId);
        BigDecimal blockedBalance = recharge.getAdvertisersWallet().getBlockedBalance();//冻结金额
        int p = blockedBalance.intValue();//转换为int类型
        int r = price.intValue();//投放广告需要的金额
        //从广告主冻结的金额里面扣钱
        int i = p - r;
        BigDecimal bigDecimal = BigDecimal.valueOf(i);//转换为BigDecimal类型
        //修改广告主的冻结金额
        AdvertisersWallet advertisersWallet = new AdvertisersWallet();
        advertisersWallet.setBlockedBalance(bigDecimal);
        advertisersWallet.setId(accountId);
        advertisersAccountService.frozen(advertisersWallet);

        //把广告主冻结的钱扣除到媒体住钱包里面
        AccountWallet accountWallet = accountWalletService.mediaRecharge(ids);
        //获取媒体住钱包现在的金额
        BigDecimal availableBalance = accountWallet.getAvailableBalance();
        int intValue = availableBalance.intValue();//转换为int类型
        //加入到媒体主钱包里面
        int i1 = intValue + r;
        BigDecimal bigDecimal1 = BigDecimal.valueOf(i1);
        //修改媒体住钱包余额
//        MediaRecharge mediaRecharge1 = new MediaRecharge();
        accountWallet.setAvailableBalance(bigDecimal1);
        accountWallet.setAccountId(accountId);
        accountWalletService.modifyMediaRecharge(accountWallet);

        return Result.success();
    }


    /*拒绝验收*/
    @RequestMapping("/no")
    @ResponseBody
    public Result no(int ids, BigDecimal price, int id) {
        Booking booking = new Booking();
        booking.setStatus(4);
        booking.setId(ids);
        bookingService.yes(booking);

        //查出投放申请单的广告主可用余额是多少
        AdvertisersAccount recharge = advertisersAccountService.recharge(id);
        BigDecimal availableBalance = recharge.getAdvertisersWallet().getBlockedBalance();
        int intValue = availableBalance.intValue();//可用余额
        int value = price.intValue();

        //拒绝验收就把冻结的钱退回到可用余额
        int i = intValue + value;
        BigDecimal bigDecimal = BigDecimal.valueOf(i);
        //修改余额
        recharge.getAdvertisersWallet().setAvailableBalance(bigDecimal);
        recharge.getAdvertisersWallet().setAdsId(id);
        advertisersAccountService.advertisersWalletModify(recharge.getAdvertisersWallet());

        //同理冻结金额也要扣除
        BigDecimal blockedBalance = recharge.getAdvertisersWallet().getBlockedBalance();
        int intValue1 = blockedBalance.intValue();//冻结金额
        int i1 = intValue1 - value;
        BigDecimal bigDecimal1 = BigDecimal.valueOf(i1);
        recharge.getAdvertisersWallet().setBlockedBalance(bigDecimal1);
        recharge.getAdvertisersWallet().setAdsId(id);
        advertisersAccountService.frozen(recharge.getAdvertisersWallet());
        return Result.success();
    }

}
